import argparse
import sys
from server.video_server import VideoServer
from client.pc_client import PCClient

def run_server(host='0.0.0.0', port=9999):
    """运行服务器"""
    print(f"启动服务器 {host}:{port}")
    server = VideoServer(host, port)
    try:
        server.start()
        # 保持主线程运行
        while True:
            try:
                input()
            except KeyboardInterrupt:
                break
    finally:
        server.stop()
        print("服务器已停止")

def run_client(host='localhost', port=9999):
    """运行客户端"""
    print(f"连接到服务器 {host}:{port}")
    client = PCClient(host, port)
    client.run()

def main():
    parser = argparse.ArgumentParser(description='视频流传输应用')
    parser.add_argument('mode', choices=['server', 'client'], 
                       help='运行模式：server(服务器) 或 client(客户端)')
    parser.add_argument('--host', default='0.0.0.0',
                       help='服务器主机地址，默认为0.0.0.0')
    parser.add_argument('--port', type=int, default=9999,
                       help='服务器端口，默认为9999')

    args = parser.parse_args()

    try:
        if args.mode == 'server':
            run_server(args.host, args.port)
        else:
            # 客户端模式下，如果host是0.0.0.0，则改为localhost
            client_host = 'localhost' if args.host == '0.0.0.0' else args.host
            run_client(client_host, args.port)
    except Exception as e:
        print(f"错误: {str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    main() 